package com.swanclouds.redfish.event;

import com.google.common.base.Preconditions;
import com.swanclouds.api.Apis;
import com.swanclouds.api.event.EventResourceService;
import com.swanclouds.api.event.SubscriptionsService;
import com.swanclouds.core.transport.ClientConstants;
import com.swanclouds.model.common.ActionResponse;
import com.swanclouds.model.event.Event;
import com.swanclouds.redfish.common.redfish.SubmitTestEvent;
import com.swanclouds.redfish.event.domain.EventModel;
import com.swanclouds.redfish.event.domain.ServiceEnabled;
import com.swanclouds.redfish.internal.BaseRedfishService;

import java.util.Map;

/**
 * @author 李文强
 * description 事件资源服务实现类
 */
public class EventResourceServiceImpl extends BaseRedfishService implements EventResourceService {

    /**
     * 查询服务器事件服务资源。
     * @return Event
     */
    @Override
    public Event get() {
        return get(EventModel.class,uri(ClientConstants.PATH_EVENTSERVICE)).execute();
    }

    /**
     *查询事件订阅集合资源
     * @return SubscriptionsService
     */
    @Override
    public SubscriptionsService Subscriptions() {
        return Apis.get(SubscriptionsService.class);
    }

    /**
     * 测试事件接口 未通
     * @param submitTestEvent 所需的参数数据
     * @return ActionResponse
     */
    @Override
    public ActionResponse SubmitTestEvent(SubmitTestEvent submitTestEvent) {
        Preconditions.checkNotNull(submitTestEvent, "submitTestEvent is null");
        Preconditions.checkNotNull(submitTestEvent.getEventType(), "EventType is null");
        Preconditions.checkNotNull(submitTestEvent.getEventId(), "EventId is null");
        Preconditions.checkNotNull(submitTestEvent.getEventTimestamp(), "EventTimestamp is null");
        Preconditions.checkNotNull(submitTestEvent.getSeverity(), "Severity is null");
        Preconditions.checkNotNull(submitTestEvent.getMessage(), "Message is null");
        Preconditions.checkNotNull(submitTestEvent.getMessageId(), "MessageId is null");
        Preconditions.checkNotNull(submitTestEvent.getMessageArgs(), "MessageArgs is null");
        Preconditions.checkNotNull(submitTestEvent.getOriginOfCondition(), "OriginOfCondition is null");
        Event event = get();
        Map<String, Object> action = event.getAction();
        if (action.containsKey("#EventService.SubmitTestEvent") && action.get("#EventService.SubmitTestEvent")!=null){
            Map<String,Object> eventSubmitTestEvent = (Map<String, Object>) action.get("#EventService.SubmitTestEvent");
            if (eventSubmitTestEvent.containsKey("target") && eventSubmitTestEvent.get("target")!=null){
                return post(ActionResponse.class,uri(eventSubmitTestEvent.get("target").toString())).entity(submitTestEvent).execute();
            }
        }
        return null;
    }

    /**
     * 修改服务器当前事件服务资源
     * @param  serviceEnabled 事件上报开关状态
     * @return  com.swanclouds.model.event.Event
     **/
    @Override
    public Event update(ServiceEnabled serviceEnabled) {
        return patch(EventModel.class,uri(ClientConstants.PATH_EVENTSERVICE_SUBSCRIPTIONS)).entity(serviceEnabled).execute();
    }

}
